package com.sduept.nwld.dataserver.manager.fault;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sduept.bigdata.risk.service.RiskAlarmMessage4PermissionQueryManager;
import com.sduept.bigdata.risk.service.RiskAlarmMessageQueryManager;
import com.sduept.message.entity.AlarmMessage;
import com.sduept.permission.entity.DepartmentEntityBean;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.entity.Principal;
import com.sduept.permission.entity.RoleEntityBean;
import com.sduept.permission.enums.RoleTypeEnum;
import com.sduept.permission.manager.DepartManager;
import com.sduept.permission.manager.PersonnelManager;

@Service
public class AlarmResourcePermissFacde {
	@Autowired
	private RiskAlarmMessage4PermissionQueryManager rpam;
	@Autowired
	private RiskAlarmMessageQueryManager raqm;
	@Autowired
	private DepartManager dm;
	@Autowired
	private PersonnelManager pm;
	
	//====================================================告警信息查询接口=====================================	
	/**
	 * 获得有效的二次设备告警信息 (即除去status=1 和 timeliness超时限的记录)如果是管理用户，不进行权限过滤，否则根据所属权限过滤查询结果
	 * @param userCode
	 * @return
	 */
	public List<AlarmMessage> getSecondaryAlarmMessage(String userCode) {
		Principal principal = getPrincipal(userCode);
		if(principal.isSystemManager()){
			return raqm.getSecondaryAlarmMessage();
		}
		return rpam.getSecondaryAlarmMessage(principal);
	}
	
	/**
	 * 获得有效的一次设备告警信息（即除去status=1 和 timeliness超时限的记录）如果是管理用户，不进行权限过滤，否则根据所属权限过滤查询结果
	 * @param userCode
	 * @return
	 */
	public List<AlarmMessage> getPrimaryAlarmMessage(String userCode) {
		Principal principal = getPrincipal(userCode);
		if(principal.isSystemManager()){
			return raqm.getPrimaryAlarmMessage();
		}
		return rpam.getPrimaryAlarmMessage(principal);
	}
	
	/**
	 * 根据消息类型，变电站，时间段查询告警消息，如果是管理用户，不进行权限过滤，否则根据所属权限过滤查询结果
	 * @param userCode
	 * @param msgType
	 * @param stationName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public List<AlarmMessage> getAlarmMessageByTypeStationDate(String userCode,String msgType, String stationName,
			Date startTime, Date endTime){
		Principal principal = getPrincipal(userCode);
		if(principal.isSystemManager()){
			return raqm.getAlarmMessageByTypeStationDate(msgType, stationName, startTime, endTime);
		}
		return rpam.getAlarmMessageByTypeStationDate(principal, msgType, stationName, startTime, endTime);
	}
	/**
	 * 获取身份信息
	 * @param userCode
	 * @return
	 */
	private Principal getPrincipal(String userCode){
		Principal principal = new Principal();
		PersonnelEntityBean person = pm.findPersonnelByCode(userCode);
		Set<RoleEntityBean> roles = person.getRoles();
		for(RoleEntityBean role:roles){
			if(RoleTypeEnum.system.toString().equals(role.getType())){
				principal.setSystemManager(true);
				return principal;
			}
		}
		List<DepartmentEntityBean> list = dm.recursiveFindParentDepartments(person);
		for(DepartmentEntityBean depart:list){
			principal.getDepartIds().add(depart.getId());
		}
		return principal;
	}
}
